package com.l024.easyandroidlib.dialog.view;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

/**
 * @author wu_ming_zhi_bei
 * @date 2020/2/26 11:41
 * @Notes  自定义Dialog
 */
public class DialogView extends Dialog {

    public DialogView(Context mContext, int layout, int style, int gravity) {
        super(mContext, style);
        setContentView(layout);
        Window window = getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.gravity = gravity;
        window.setAttributes(layoutParams);
    }

    /**
     * 显示在某个控件底部
     * @param mContext
     * @param layout
     * @param style
     */
    public DialogView(Context mContext, int layout, int style, View view) {
        super(mContext, style);
        setContentView(layout);
        Window window = getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        //获取通知栏高度
        int notificationBar = Resources.getSystem().getDimensionPixelSize(Resources.getSystem().getIdentifier("status_bar_height", "dimen", "android"));
        int[] location = new int[2] ;
        view.getLocationInWindow(location); //获取在当前窗体内的绝对坐标
        view.getLocationOnScreen(location);//获取在整个屏幕内的绝对坐标
        layoutParams.x=0;//对 dialog 设置 x 轴坐标
        layoutParams.y = location [1] + view.getHeight()-notificationBar+20; //对dialog设置y轴坐标
        layoutParams.gravity = Gravity.TOP;
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(layoutParams);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if(onDialogListener!=null){
            onDialogListener.onDialogShow();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(onDialogListener!=null){
            onDialogListener.onDialogHide();
        }
    }

    //监听
    private OnDialogListener onDialogListener = null;
    public interface OnDialogListener{
        void onDialogShow();
        void onDialogHide();
    }

    public void setOnDialogListener(OnDialogListener onDialogListener){
        this.onDialogListener = onDialogListener;
    }
}
